import {
  getQuestionsType,
    insertQuestionsType,
    delQuestionsType,
    fetchSubject,
    fetchExamType,
    createQuestions,
    fetchQuestions,
    updateQuestions
} from '../services/questions'
export default {

    namespace: 'questions',
  
    state: {
        questionsType: [], // 试题类型
        subject: [], // 所有课程
        examType: [], // 考试类型
        questionsList: {
          data: [],
          page: 0,
          pageSize: 10
        }
      },
    subscriptions: {
      setup({ dispatch, history }) {  // eslint-disable-line
        dispatch({
          type: 'fetchInitData'
        })
      },
    },
  
    effects: {
      *fetchInitData(actions, { call, put }) {
        const promiseAll = [
          getQuestionsType(),
          fetchSubject(),
          fetchExamType()
        ]
        const initData = () => Promise.all(promiseAll)
        const res = yield call(initData);
        console.log(res)
        yield put({
          type: 'changeState',
          payload: {
            questionsType: res[0].data, // 试题类型
            subject: res[1].data, // 所有课程
            examType: res[2].data // 考试类型
          }
        })
      },
      *fetchQuestions ({payload}, {call, put}) {
        const res = yield call(fetchQuestions, payload)
        console.log(res)
        yield put({
          type: 'changeState',
          payload: {
            questionsList: {
              data: res.data,
              page: 0,
              pageSize: 10
            }
          }
        })
      },
      *createQuestions({payload}, {call, put}){
         const res=yield call(createQuestions,payload)
         return Promise.resolve(res)
      },
      *fetch({ payload }, { call, put }) {  // eslint-disable-line
        yield put({ type: 'save' });
      },
    },
  
    reducers: {
      changeState(state, action) {
        return { ...state, ...action.payload };
      },
    },
  
  };